package site.yunnong.atvris.common.utils;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 列表工具类
 * @author zjh
 * @date 2021/8/14 22:13
 */
public class ListUtils {

    /**
     * 内存随机取数据
     * @param list
     * @param num
     * @return
     */
    public static List getRandomList(List list, int num) {
        List result = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)) {
            return result;
        }
        if (list.size() < num) {
            return list;
        } else {
            Random random = new Random();
            for (int i = 0; i < num; i++) {
                int intRandom = random.nextInt(list.size() - 1);
                result.add(list.get(intRandom));
                list.remove(list.get(intRandom));
            }
            return result;
        }
    }
}
